package com.example.weathermvvm.util

import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.databinding.BindingAdapter
import androidx.databinding.DataBindingUtil
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.bumptech.glide.Glide
import com.example.weathermvvm.R
import com.example.weathermvvm.data.model.weather.Weather
import com.example.weathermvvm.databinding.ForecastItemBinding

/**
 *
 * @Author： LJH
 * @Time： 2022/8/15
 * @description：
 */
@BindingAdapter("bind:loadBingPic")
fun ImageView.loadBingPic(url:String?){
    url?.let {
        Glide.with(context).load(it).into(this)
    }
}

@BindingAdapter("bind:colorSchemeResources")
fun SwipeRefreshLayout.colorSchemeResources(resId:Int){
    setColorSchemeResources(resId)
}

@BindingAdapter("bind:showForecast")
fun LinearLayout.showForecast(weather:Weather?) = weather?.let {
    removeAllViews()
    for (forecast in it.forecastList){
        val view = LayoutInflater.from(context).inflate(R.layout.forecast_item,this,false)
        DataBindingUtil.bind<ForecastItemBinding>(view)?.forecast = forecast
        addView(view)
    }
}